<template>
    <div class="wrapper">
        <div class="search">
            <van-icon @click="$router.go(-1)" size="22px" name="arrow-left" />
            <van-search v-model="that.query" show-action placeholder="请输入搜索关键词" @search="onSearch" @cancel="onCancel"
                @input="flg" />
        </div>
        <!-- <div class="box" v-if="flag">
            <div v-show="aa" class="box-cont" v-for="(item,index) in listData.list" :key="index">
                <img style="width:110px;height:68px" :src="item.cover_img" alt />
                <div class="box-cont-item">
                    <p>{{item.title}}</p>
                    <div class="box-item-text">
                        <span>{{item.sales_num}}人已报名</span>
                        <span v-if="item.price == 0">免费</span>
                        <span v-else>
                            <img style="width:12px;height:14px" src="" />
                            1.00
                        </span>
                    </div>
                    <div class="yibao">
                        <img src alt />
                    </div>
                </div>
            </div>
            <div class="box" v-if="!aa">
                <div style="height:200px"></div>
            </div>
        </div> -->
        <div class="box" v-if="that.flag">
            <div v-show="that.aa" class="jp-box" v-for="(item,index) in that.listData.list" :key="index">
                <div class=" jp-box-item1">{{item.title}}</div>
                <div class="jp-box-item2">共点击量</div>
                <div class="jp-box-item3">
                    <img :src="item.cover_img" alt />
                </div>
                <div class="jp-box-item4">
                    <p>{{item.id}}人已报名</p>
                    <span v-if="item.price == 0">0.00</span>
                    <div v-else>
                        <span>{{item.price}}</span>
                    </div>
                </div>
            </div>
            <div class="box" v-if="!that.aa">
                <div style="height:200px">无数据</div>
            </div>
        </div>
        <div class="box" v-else>
            <div class="box-item">
                <span style="font-size:18px;">历史搜索</span>
                <van-icon @click="clears" name="delete-o" />
            </div>
            <div class="box-item2">
                <van-tag closeable size="medium" type="primary" @close="close(index)" @click="dian(item)"
                    v-for="(item,index) in that.arr" :key="index">{{item}}</van-tag>
            </div>
        </div>

    </div>
</template>
  
<script setup>
import { getSearchPort } from '@/api/course'
import { reactive, onMounted } from 'vue';
let that = reactive({
    query: '',
    flag: false,
    arr: [],
    limit: 10,
    page: 1,
    course_type: 0,
    listData: [],
    aa: false
})
onMounted(() => {
    let data = localStorage.getItem('arr')
    if (data) {
        that.arr = JSON.parse(data)
    }
})

// 点击历史记录搜索
let dian = (val) => {
    that.query = val
    onSearch(val)
}
// 删除历史记录
let close = (index) => {
    that.arr.splice(index, 1)
    // that.show = false
}
// 搜索
let onSearch = async (val) => {
    // console.log(val)
    if (that.arr.indexOf(val) == -1) {
        that.arr.push(val)
    }
    console.log(that.arr.indexOf(val))

    localStorage.setItem('arr', JSON.stringify(that.arr))

    let res = await getSearchPort(that.limit, that.page, that.course_type, that.query)
    console.log(res)
    if (res.code == 200) {
        that.listData = res.data
    }
    if (that.listData == '') {
        that.flag = false
        that.aa = false
    } else {
        that.flag = true
        that.aa = true
    }
}

let onCancel = () => {
    console.log('取消')
    that.listData = []
    that.flag = false
    // that.aa = true
}
// 输入内容触发
let flg = (val) => {
    that.flag = true
    // console.log(val)
    if (val == '') {
        that.flag = false
    }
    that.listData = []
}
// 清空历史记录
let clears = () => {

    console.log('000')
    that.arr = []
    localStorage.removeItem('arr')
}


</script>
  
<style lang="scss" scoped>
.wrapper {
    width: 750px;
    background: #fff;
}

.search {
    width: 750px;
    height: 77px;
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.van-search {
    width: 700px;
    height: 60px;
    border-radius: 24px;
}

.box-item {
    width: 700px;
    padding: 5px 15px;
    display: flex;

    justify-content: space-between;
    align-items: center;
}

.box-item2 {
    width: 700px;
    padding: 10px 15px;
    display: flex;

    .van-tag {
        min-width: 50px;
        height: 25px;
        margin: 8px 10px;
        display: flex;
        justify-content: space-between;
        font-size: 20px;
        background: rgb(214, 179, 179);
    }
}

.box {
    width: 100vw;
    box-sizing: border-box;
    padding: 0 10px 20px;
}

.jp-box {
    width: 95vw;
    height: 259px;
    display: flex;
    flex-direction: column;
    background: #fff;
    margin: 14px 0;
    padding: 0 10px;
    border-radius: 6px;

    .jp-box-item1 {
        font-size: 25px;
        // height: 47px;
        padding-top: 35px;
    }

    .jp-box-item2 {
        height: 50px;
        line-height: 50px;
        color: #999;
        font-size: 20px;
    }

    .jp-box-item3 {
        height: 65px;
        display: flex;
        align-items: center;
        color: #999;

        img {
            border-radius: 50%;
            width: 43px;
            height: 43px;
            margin-right: 15px;
        }
    }

    .jp-box-item4 {
        border-top: 1px solid rgb(243, 241, 241);
        height: 44px;
        display: flex;
        align-items: center;
        justify-content: space-between;

        p {
            margin: 0;
            width: 290px;
            color: #999;
        }

        span {
            font-size: 20px;
            color: #eb6100;
            display: flex;
            align-items: center;
        }
    }
}
</style>
  